package com.lesso.lzh.sunflower.infra.repository.impl;

import com.baomidou.mybatisplus.mapper.EntityWrapper;
import com.baomidou.mybatisplus.plugins.Page;
import com.lesso.lzh.sunflower.domain.repository.RelEmployeeDeviceRepository;
import com.lesso.lzh.sunflower.infra.assembler.RelEmployeeDeviceAssembler;
import com.lesso.lzh.sunflower.infra.mapper.RelEmployeeDeviceMapper;
import com.lesso.lzh.sunflower.model.RelEmployeeDevice;
import com.lesso.lzh.sunflower.pojo.dto.RelEmployeeDeviceQueryDTO;
import com.lesso.lzh.sunflower.pojo.vo.RelEmployeeDeviceVO;
import org.springframework.stereotype.Repository;
import org.springframework.util.StringUtils;

import java.time.LocalDateTime;
import java.util.List;

/**
 * RelEmployeeDeviceRepositoryImpl
 *
 * @author zqz
 * @since 2025-07-05
 */
@Repository
public class RelEmployeeDeviceRepositoryImpl implements RelEmployeeDeviceRepository {
    private final RelEmployeeDeviceMapper relEmployeeDeviceMapper;

    public RelEmployeeDeviceRepositoryImpl(RelEmployeeDeviceMapper relEmployeeDeviceMapper) {
        this.relEmployeeDeviceMapper = relEmployeeDeviceMapper;
    }

    @Override
    public Long save(RelEmployeeDevice relEmployeeDevice) {
        relEmployeeDevice.setCreateTime(LocalDateTime.now());
        relEmployeeDevice.setUpdateTime(LocalDateTime.now());
        relEmployeeDevice.setDelStatus(0);
        relEmployeeDeviceMapper.insert(relEmployeeDevice);
        return relEmployeeDevice.getId();
    }

    @Override
    public Page<RelEmployeeDeviceVO> getPageList(RelEmployeeDeviceQueryDTO dto) {
        Page<RelEmployeeDeviceVO> page = new Page<>(dto.getCurrent(), dto.getSize());
        EntityWrapper<RelEmployeeDevice> wrapper = new EntityWrapper<>();
        // 按员工ID筛选
        wrapper.eq(dto.getEmployeeId() != null, "employee_id", dto.getEmployeeId());
        // 按设备编码筛选
        wrapper.eq(StringUtils.hasText(dto.getDeviceCode()), "device_code", dto.getDeviceCode());
        // 按授权状态筛选
        wrapper.eq(dto.getAuthorizationStatus() != null, "authorization_status", dto.getAuthorizationStatus());
        // 过滤未删除数据
        wrapper.eq("del_status", 0);
        
        List<RelEmployeeDevice> rels = relEmployeeDeviceMapper.selectPage(page, wrapper);
        return page.setRecords(RelEmployeeDeviceAssembler.INSTANCE.entityListToVOList(rels));
    }

    @Override
    public Page<RelEmployeeDeviceVO> getCustomPageList(RelEmployeeDeviceQueryDTO dto) {
        Page<RelEmployeeDeviceVO> page = new Page<>(dto.getCurrent(), dto.getSize());
        List<RelEmployeeDeviceVO> list = relEmployeeDeviceMapper.selectPageList(page, dto);
        return page.setRecords(list);
    }

    @Override
    public void removeById(Long id) {
        relEmployeeDeviceMapper.deleteById(id);
    }

    @Override
    public RelEmployeeDevice selectById(Long id) {
        return relEmployeeDeviceMapper.selectById(id);
    }

    @Override
    public void update(RelEmployeeDevice relEmployeeDevice) {
        relEmployeeDevice.setUpdateTime(LocalDateTime.now());
        relEmployeeDeviceMapper.updateSelective(relEmployeeDevice);
    }

    @Override
    public List<RelEmployeeDevice> listByEmployeeId(Long employeeId) {
        EntityWrapper<RelEmployeeDevice> wrapper = new EntityWrapper<>();
        wrapper.eq("employee_id", employeeId)
                .eq("del_status", 0); // 只查询未删除的记录
        return relEmployeeDeviceMapper.selectList(wrapper);
    }
}